package com.treeroot.springbootwithelastic.com.treeroot.es.service.impl;

import com.treeroot.springbootwithelastic.com.treeroot.es.service.ProductService;
import com.treeroot.springbootwithelastic.com.treeroot.es.tools.ComplexPagingSearchCmd;
import com.treeroot.springbootwithelastic.com.treeroot.es.tools.DocAction;
import com.treeroot.springbootwithelastic.com.treeroot.es.tools.JdHtmlParsesTools;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.*;

@Slf4j
@Service
public class ProductServiceImpl implements ProductService {
    @Resource
    private DocAction docAction;
    @Value("${esconfig.jdIndex}")
    private String jdIndex;


    @Override
    public BulkResponse export(String keyword) throws Exception {
        return docAction.batchAddDoc(JdHtmlParsesTools.parseJd(keyword), jdIndex);
    }

    @Override
    public Map<String, Object> search(String keyword, Integer currentPage, Integer pageSize) throws IOException {
        return docAction.searchReqByPage(
                new ComplexPagingSearchCmd()
                        .setDocIndex(jdIndex)
                        .setCurrentPage(currentPage)
                        .setPageSize(pageSize)
                        .setKeyword(keyword)
                        .setMatchQueryBuilder(new MatchQueryBuilder("title", keyword))
        );
    }
}
